package cn.zhagzheguo.xk.test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.ibatis.session.SqlSession;
import org.springframework.transaction.annotation.Transactional;

import cn.zhagzheguo.xk.core.SessionFactory;
import cn.zhagzheguo.xk.model.Course;
import cn.zhagzheguo.xk.model.Teacher;
import cn.zhagzheguo.xk.util.excel.XlsParser;
import cn.zhagzheguo.xk.util.excel.model.TeacherTmp;

public class ImportExcelToDb {

	private static List<Teacher> teachers = null;

	/**
	 * insert Teacher.
	 * *******************************************/
	public static void addTeacher(String tXlsPath, int start, int end) {
		teachers = readTeacher(tXlsPath, start, end);

		for (int i = 0; i < teachers.size(); i++) {
			insert(teachers.get(i));
		}
	}

	public static List<Teacher> readTeacher(String tXlsPath, int start, int end) {
		List<Teacher> list = new ArrayList<>();

		Random ran = new Random();

		List<String> headIcons = readHeadIcon("E:\\bks_xk\\file\\headIcon\\");

		List<Object> tTmp = readTeacherXls(tXlsPath, start, end);

		Teacher t = null;
		for (int i = 0; i < tTmp.size(); i++) {
			TeacherTmp tt = (TeacherTmp) tTmp.get(i);
			String name = tt.getName().substring(0, tt.getName().indexOf("(") - 1);
			String tno = tt.getName().substring(tt.getName().indexOf("(") + 1, tt.getName().indexOf(")"));

			boolean isIn = false;
			for (Teacher te : list) {
				if (te.getTno().equals(tno)) {
					isIn = true;
					break;
				}
			}

			if (isIn) {
				continue;
			}

			t = new Teacher();
			t.setAge(25 + ran.nextInt(30));
			t.setDescription("description");
			t.setHeadIcon(headIcons.get(i % headIcons.size()));
			t.setGender(ran.nextInt(1));
			t.setName(name);
			t.setTno(tno);
			System.out.println(t.toString());
			list.add(t);
		}
		return list;
	}

	@Transactional
	public static void insert(Teacher teacher) {
		SqlSession session = SessionFactory.getSession();
		try {
			session.insert("cn.zhagzheguo.xk.mapper.TeacherMapper.insert", teacher);
			session.commit(false);
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback(false);
		}
	}

	public static List<Object> readTeacherXls(String tXlsPath, int start, int end) {
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(7, "name");

		XlsParser parser = new XlsParser(tXlsPath, map, TeacherTmp.class);

		parser.setStartRow(start);
		parser.setLastRow(end);

		List<Object> list = parser.parse();

		for (int i = 0; i < list.size(); i++) {
			System.out.println(i + ":" + ((TeacherTmp) list.get(i)).toString());
		}

		return list;
	}

	public static List<String> readHeadIcon(String iconPath) {
		List<String> list = new ArrayList<>();

		File file = new File(iconPath);
		String[] images = file.list();

		for (String s : images) {
			String imagePath = "file/download/headIcon/" + s;
			System.out.println(imagePath);
			list.add(imagePath);
		}

		return list;
	}

	@Transactional
	public static String readTeacherId(String tno) {
		SqlSession session = SessionFactory.getSession();
		try {
			List<String> list = session.selectList("cn.zhagzheguo.xk.mapper.CourseMapper.select_id", tno);
			return list.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * insert courses
	 * ****************************************/

	public static void addCourse(String xls, int start, int end) {
		/**
		 * 读取图片
		 */
		List<String> images = readImage("E://bks_xk//file//images//");

		List<Object> list = readXls(xls, start, end);

		for (int i = 0; i < list.size(); i++) {
			Course c = (Course) list.get(i);
			c.setIcon(images.get(i % images.size()));
			c.setTeacher1Id(i % teachers.size());
			insert(c);
		}
	}

	public static List<Object> readXls(String xlsPath, int start, int end) {

		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(1, "openedDep");
		map.put(2, "cno");
		map.put(3, "kxh");
		map.put(4, "name");
		map.put(5, "credit");
		map.put(6, "hours");
		map.put(9, "capacity");
		map.put(10, "classes");
		map.put(11, "weeks");
		map.put(12, "place");
		map.put(13, "clazz");
		map.put(14, "grade");
		map.put(15, "nature");

		XlsParser parser = new XlsParser(xlsPath, map, Course.class);

		parser.setStartRow(start);
		parser.setLastRow(end);

		List<Object> list = parser.parse();

		for (int i = 0; i < list.size(); i++) {
			System.out.println(i + ":" + ((Course) list.get(i)).toString());
		}

		return list;
	}

	@Transactional
	public static void insert(Course course) {
		SqlSession session = SessionFactory.getSession();
		try {
			session.insert("cn.zhagzheguo.xk.mapper.CourseMapper.insert", course);
			session.commit(false);
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback(false);
		}
	}

	public static List<String> readImage(String path) {
		List<String> list = new ArrayList<>();

		File file = new File(path);
		String[] images = file.list();

		for (String s : images) {
			String imagePath = "file/download/images/" + s;
			System.out.println(imagePath);
			list.add(imagePath);
		}

		return list;
	}

	/***************************************************/

	public static void main(String[] args) {
		/**
		 * 读取该文件夹下的所有文件
		 */
		File ePath = new File("E://");
		String[] files = ePath.list();

		int start = 2;
		int end = 20;
		for (String path : files) {
			if (path.endsWith(".xls")) {
				String abs = "E://" + path;
				addTeacher(abs, start, end);
				addCourse(abs, start, end);
			}
		}

	}

}
